/* Copyright (c) 2008, RDFfs - Andrea Marchesini <baku@morlardf.net>
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *     * Redistributions of source code must retain the above copyright
 *       notice, this list of conditions and the following disclaimer.
 *     * Redistributions in binary form must reproduce the above copyright
 *       notice, this list of conditions and the following disclaimer in the
 *       documentation and/or other materials provided with the distribution.
 *     * Neither the name of the <organization> nor the
 *       names of its contributors may be used to endorse or promote products
 *       derived from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
 * A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 
 */

#ifdef HAVE_CONFIG_H
#include <config.h>
#else
# error Use configure; make; make install
#endif

#include <rdffs.h>

static int write_and_exec (gchar ** argv);

int
main (int argc, char **argv)
{
  GError *error = NULL;

  g_thread_init (NULL);

  if (argc != 3)
    {
      fprintf (stderr,
	       PACKAGE " " VERSION " - Author: " AUTHOR "  - License: "
	       LICENSE "\n\n");
      fprintf (stderr, "%s <MountDirectory> <RDFDocument>\n", argv[0]);
      return 1;
    }

  memset (&rdffs_input, 0, sizeof (rdffs_input));

  if (g_file_get_contents
      (argv[2], &rdffs_input.input, &rdffs_input.input_size, &error) == FALSE)
    {
      fprintf (stderr, "Error reading the RDF document '%s': %s\n", argv[2],
	       error->message);
      return FALSE;
    }

  rdffs_input.filename = argv[2];
  rdffs_input.mutex_reader = g_mutex_new ();
  rdffs_input.mutex_writer = g_mutex_new ();

  if (rdffs_parse
      (rdffs_input.input, rdffs_input.filename, RDF_FORMAT_XML,
       &rdffs_input.error, &rdffs_input.error_size) == TRUE)
    return write_and_exec (argv);

  if (rdffs_input.error)
    {
      g_free (rdffs_input.error);
      rdffs_input.error = NULL;
      rdffs_input.error_size = 0;
    }

  if (rdffs_parse
      (rdffs_input.input, rdffs_input.filename, RDF_FORMAT_TURTLE,
       &rdffs_input.error, &rdffs_input.error_size) == TRUE)
    return write_and_exec (argv);

  if (rdffs_input.error)
    {
      g_free (rdffs_input.error);
      rdffs_input.error = NULL;
      rdffs_input.error_size = 0;
    }

  if (rdffs_parse
      (rdffs_input.input, rdffs_input.filename, RDF_FORMAT_NTRIPLES,
       &rdffs_input.error, &rdffs_input.error_size) == TRUE)
    return write_and_exec (argv);

  fprintf (stderr, "Error reading the RDF document '%s': %s\n", argv[2],
	   rdffs_input.error);

  rdffs_destroy (NULL);
  return 1;
}

static int
write_and_exec (gchar ** argv)
{
  /* Write the XML buffer: */
  if (rdffs_write
      (rdffs_input.filename, RDF_FORMAT_XML, &rdffs_input.format_xml,
       &rdffs_input.format_xml_size, &rdffs_input.error,
       &rdffs_input.error_size) == TRUE
      /* Write the TURTLE buffer: */
      && rdffs_write (rdffs_input.filename, RDF_FORMAT_TURTLE,
		      &rdffs_input.format_turtle,
		      &rdffs_input.format_turtle_size, &rdffs_input.error,
		      &rdffs_input.error_size) == TRUE
      /* Write the NTRIPLES buffer: */
      && rdffs_write (rdffs_input.filename, RDF_FORMAT_NTRIPLES,
		      &rdffs_input.format_ntriples,
		      &rdffs_input.format_ntriples_size, &rdffs_input.error,
		      &rdffs_input.error_size) == TRUE)

    return fuse_main (2, argv, &rdffs_oper, NULL);

  fprintf (stderr, "Error writing the RDF document '%s': %s\n", argv[2],
	   rdffs_input.error);

  /* Error: */
  return 1;
}

/* EOF */
